import jieba
import logging
from wordcloud import WordCloud


def get_text(name: str):
    """
    通过文件名读取问价内容
    :param name: 要读取文件的名称
    :return: 文件内容
    """
    with open(name, "r", encoding="utf-8") as f:
        content = f.read()
        return content


def clean_text(content: str):
    """
    删除字符串中无意义的词
    :param content: 要出来的文本内容
    :return: 处理后的问题
    """
    word_list = ["有限", "责任", "公司"]

    for word in word_list:
        content = content.replace(word, '')

    return content


def segment_word(content: str):
    """

    :param content:
    :return:
    """
    jieba.setLogLevel(log_level=logging.INFO)
    # seg_list = jieba.lcut(content)
    segment = jieba.cut(content)

    return " ".join(segment)


def generate_wordcloud(words):
    """
    将传入的词生成词云图片
    :param words: 要生成词云的词
    :return: 无
    """
    picture = WordCloud(font_path="../wordcloud_tutorial/fonts/msyhl.ttc", background_color="white", width=800,
                        height=600)
    picture.generate(words)
    picture.to_file("公司命名习惯.jpeg")


if __name__ == '__main__':
    text = get_text("./namelist.txt")
    text = clean_text(text)
    words = segment_word(text)
    generate_wordcloud(words)
